package com.shushan.manager.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.shushan.db.RelaxTimeDao;
import com.shushan.manager.bean.RelaxTimeBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.ControlUtil;
import com.shushan.util.StringTools;
import com.shushan.util.UmengEventUtil;

import java.util.List;

/**
 * @ClassName: TimeManagerAdapter
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-07-19
 */
public class TimeManagerAdapter extends BaseAdapter{

    private static final String TAG = "TimeManagerAdapter";

    private Context context;
    private RelaxTimeDao relaxTimeDao;
    private List<RelaxTimeBean> relaxTimeBeanList;

    public TimeManagerAdapter(Context context) {
        this.context = context;
        relaxTimeDao = new RelaxTimeDao(context);
        relaxTimeBeanList = relaxTimeDao.queryForAll();
    }

    @Override
    public int getCount() {
        return relaxTimeBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return relaxTimeBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(context).inflate(R.layout.time_manager_list_item, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.nameTextView = convertView.findViewById(R.id.time_mg_item_name_tv);
            holder.timeTextView = convertView.findViewById(R.id.time_mg_item_time_tv);
            holder.weekTextView = convertView.findViewById(R.id.time_mg_item_week_tv);
            holder.openSwitch = convertView.findViewById(R.id.time_mg_item_open_switch);
            holder.deleteImg = convertView.findViewById(R.id.time_mg_item_delete_img);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final RelaxTimeBean relaxTimeBean = relaxTimeBeanList.get(position);
        Log.i(TAG,"frequDay=" + relaxTimeBean.frequDay);
        holder.nameTextView.setText(relaxTimeBean.planName);
        holder.timeTextView.setText(StringTools.formatTime(relaxTimeBean.startHour,relaxTimeBean.startMin) + "-"
                + StringTools.formatTime(relaxTimeBean.endHour,relaxTimeBean.endMin));
        holder.openSwitch.setChecked(relaxTimeBean.isAction);
        holder.weekTextView.setText(getFrequDays(relaxTimeBean.frequDay));

        holder.openSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                relaxTimeDao.update(relaxTimeBean.id,isChecked);

                if(isChecked){
                    ControlUtil.setTimeMgAlarm(context,relaxTimeBean.id,relaxTimeBean.frequDay,relaxTimeBean.startHour,
                            relaxTimeBean.startMin,relaxTimeBean.endHour,relaxTimeBean.endMin);
                }else {
                    ControlUtil.cancelTimeMgAlarm(context,relaxTimeBean.id);
                }

                UmengEventUtil.onEvent(BabyApplication.getInstance(),"time_manager_click_id","open");
            }
        });

        holder.deleteImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relaxTimeDao.delete(relaxTimeBean);
                if(relaxTimeBean.isAction){
                    ControlUtil.cancelTimeMgAlarm(context,relaxTimeBean.id);
                }

                notifyDataChanged();
                UmengEventUtil.onEvent(BabyApplication.getInstance(),"time_manager_click_id","delete");
            }
        });

        return convertView;
    }

    public static class ViewHolder{
        TextView nameTextView;
        TextView timeTextView;
        TextView weekTextView;
        Switch openSwitch;
        ImageView deleteImg;
    }

    public void notifyDataChanged() {
        relaxTimeBeanList = relaxTimeDao.queryForAll();

        notifyDataSetChanged();
    }

    private String getFrequDays(int frequDay){
        String[] weekArray = {"周日","周一","周二","周三","周四","周五","周六"};
        String days = "重复:";

        if(frequDay == 127){
            return days + " 每天";
        }

        for (int i = 0;i < 7;i++){
            if((frequDay & (1<<i)) > 0){
                days += " " + weekArray[i];
            }
        }

        return days;
    }
}
